package accountManagementNew.service.impl;

import accountManagementNew.common.base.Result;
import accountManagementNew.common.dto.UserDTO;
import accountManagementNew.common.entity.UserEntity;
import accountManagementNew.common.enums.ResultCodeEnum;
import accountManagementNew.common.enums.UserStatusEnum;
import accountManagementNew.common.pojo.User;
import accountManagementNew.common.vo.UserVO;
import accountManagementNew.mapper.UserMapper;
import accountManagementNew.service.IUserService;
import accountManagementNew.utils.PasswordUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * @Author：lihonbin
 * @version：2025/9/3
 * @description 用户服务
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    /**
     * 判断用户名是否存在
     *
     * @param username 用户名
     * @return true:存在 false:不存在
     */
    @Override
    public Boolean queryUserNameIsHave(String username) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        QueryWrapper<User> wrapper = userQueryWrapper.eq(UserEntity.username, username)
                .ne(UserEntity.status, UserStatusEnum.CANCELED);
        return count(wrapper) > 0;
    }

    /**
     * 注册用户
     *
     * @param userDTO 用户信息
     * @return 注册结果
     */
    @Override
    public Boolean registeredUser(UserDTO userDTO) {
        // 将UserDTO转换为User实体
        User user = new User();
        user.setUsername(userDTO.getUsername());
        user.setPassword(PasswordUtil.encode(userDTO.getPassword()));
        user.setEmail(userDTO.getEmail());
        user.setPhone(userDTO.getPhone());
        // 设置随机的昵称
        user.setNickname(userDTO.getUsername() + new Date().getTime());
        user.setGender(userDTO.getGender());
        user.setStatus(UserStatusEnum.NORMAL); // 新注册用户状态为正常
        user.setRegisterTime(LocalDateTime.now()); // 设置注册时间
        return save(user);
    }

    /**
     * 登录
     *
     * @param user 用户信息
     * @return 用户的全部信息
     */
    @Override
    public UserVO login(UserDTO user) {
        // 更具用户的手机去查询用户信息
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        User userInfo = getOne(userQueryWrapper.eq(UserEntity.username, user.getUsername())
                .eq(UserEntity.status, UserStatusEnum.NORMAL)
        );
        if (userInfo != null) {
            // 验证密码的准确性
            if (PasswordUtil.matches(user.getPassword(), userInfo.getPassword())) {
                // 密码校验成功, 返回登录成功，设置最后登录时间
                userInfo.setLastLogin(LocalDateTime.now());
                updateById(userInfo);
                UserVO userVO = new UserVO();
                BeanUtils.copyProperties(userInfo, userVO);
                return userVO;
            }
        }
        return null;
    }

    /**
     * 修改用户密码
     *
     * @param user 用户信息
     * @return 修改结果
     */
    @Override
    public boolean changeUserPassword(User user) {
        // 对修改的密码进行加密
        user.setPassword(PasswordUtil.encode(user.getPassword()));
        return updateById(user);
    }
}